package com.ewzj.bigdata.mvp.model;

import android.app.Application;

import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.mvp.model.api.service.CommonService;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqIdBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.ArticleBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.NewBean;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.VideoDetailContract;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;


@ActivityScope
public class VideoDetailModel extends BaseModel implements VideoDetailContract.Model {
    private Gson mGson;
    private Application mApplication;
    private CommonService service;
    @Inject
    public VideoDetailModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
        super(repositoryManager);
        this.mGson = gson;
        this.mApplication = application;
        this.service=repositoryManager.obtainRetrofitService(CommonService.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
        this.service=null;
    }

    @Override
    public Observable<ArticleBean> getArticleDetail(String id) {
        ReqIdBean reqIdBean=new ReqIdBean(id);
        return service.getArticleDetail(Apputils.json2Body(mGson.toJson(reqIdBean)))
                .map(new Function<BaseData<ArticleBean>, ArticleBean>() {
                    @Override
                    public ArticleBean apply(BaseData<ArticleBean> articleBeanBaseData) throws Exception {
                        return articleBeanBaseData.getData();
                    }
                });
    }

    @Override
    public Observable<List<NewBean>> getGuessLike(String id) {
        ReqIdBean reqIdBean=new ReqIdBean(id);
        return service.guessLikeList(Apputils.json2Body(mGson.toJson(reqIdBean)))
                .map(new Function<BaseData<List<NewBean>>, List<NewBean>>() {
                    @Override
                    public List<NewBean> apply(BaseData<List<NewBean>> listBaseData) throws Exception {
                        return listBaseData.getData();
                    }
                });
    }
}